package redisinit

import (
	"context"
	"fmt"
	"shop_srv/shop"
	"strconv"
)

func CartAdd(UserId, ShopId, ShopStock int64, ShopPrice float64, ShopStatus, ShopName string) error {
	key := fmt.Sprintf("cart:user_id_%d:shop_id_%d", UserId, ShopId)
	return Rdb.HMSet(context.Background(), key, map[string]interface{}{
		"UserId":     UserId,
		"ShopId":     ShopId,
		"ShopStock":  ShopStock,
		"ShopPrice":  ShopPrice,
		"ShopStatus": ShopStatus,
		"ShopName":   ShopName,
	}).Err()
}
func CartList(UserId int64) (cartList []*shop.Item, err error) {
	key := fmt.Sprintf("cart:user_id_%d:shop_id_*", UserId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, s := range keys {
		all := Rdb.HGetAll(context.Background(), s).Val()
		UserId, _ := strconv.Atoi(all["UserId"])
		ShopId, _ := strconv.Atoi(all["ShopId"])
		ShopStock, _ := strconv.Atoi(all["ShopStock"])
		ShopPrice, _ := strconv.ParseFloat(all["ShopStock"], 64)
		ShopName := all["ShopName"]
		ShopStatus := all["ShopStatus"]
		cartList = append(cartList, &shop.Item{
			UserId:     int64(UserId),
			ShopId:     int64(ShopId),
			ShopStock:  int64(ShopStock),
			ShopPrice:  float32(ShopPrice),
			ShopName:   ShopName,
			ShopStatus: ShopStatus,
		})
	}
	return
}
